package cn.com.oceansoft.business.mobile.danger.service;


import cn.com.oceansoft.business.base.entity.Pager;
import cn.com.oceansoft.business.base.entity.Query;
import cn.com.oceansoft.business.base.entity.Result;
import cn.com.oceansoft.business.mobile.danger.entity.*;
import cn.com.oceansoft.business.mobile.danger.persistence.ICollecterMapper;
import cn.com.oceansoft.nos.common.util.web.PageUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;

/**
 * Created by stt on 2018/4/27.
 */
@Service
public class CollecterServiceImpl implements ICollecterService {

    @Resource
    private ICollecterMapper collecterMapper;

    @Override
    public Result collecterInfoSubmit(CollecterInfo collecterInfo) {
        int rt = 0;
        if(collecterInfo.getId()!=null&&!"".equals(collecterInfo.getId())){
            rt = collecterMapper.updataCollecterInfo(collecterInfo);
        }else {
            rt = collecterMapper.addCollecterInfo(collecterInfo);
        }
        if(rt>0){
            return new Result(Result.SUCC,"提交成功!");
        }else {
            return new Result(Result.FAILURE,"提交失败!");
        }
    }

    @Override
    public CollecterInfo findByOpenid(String openId) {
        CollecterInfo c = collecterMapper.findByOpenid(openId);
        return c;
    }

    @Override
    public Pager<DangerContent> loadReportResultByPage(Query query) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        PageHelper.startPage(query.getPageNum(), query.getNumPerPage(), true);
        List<DangerContent> list = collecterMapper.loadReportResultByPage(query);
        for(DangerContent l: list){
            l.setDate(formatter.format(l.getCreatetime()));
        }
        Page<DangerContent> page = (Page<DangerContent>) list;
        return PageUtil.page2pager(page, query);
    }

    @Override
    public DangerContent getDangerContentById(String id) {
        return collecterMapper.getDangerContentById(id);
    }

    @Override
    public List<Map<String,Object>> getPhotoByDCId(String id) {
        return collecterMapper.getPhotoByDCId(id);
    }

    /**
     *审批未处理
     */
    @Override
    public Pager<DangerContent> loadListWByPage(Query query) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        PageHelper.startPage(query.getPageNum(), query.getNumPerPage(), true);
        List<DangerContent> list = collecterMapper.loadListWByPage(query);
        for(DangerContent l: list){
            l.setDate(formatter.format(l.getCreatetime()));
        }
        Page<DangerContent> page = (Page<DangerContent>) list;
        return PageUtil.page2pager(page, query);
    }

    /**
     *审批已处理 处理中
     */
    @Override
    public Pager<DangerContent> loadListYByPage(Query query) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        PageHelper.startPage(query.getPageNum(), query.getNumPerPage(), true);
        List<DangerContent> list = collecterMapper.loadListYByPage(query);
        for(DangerContent l: list){
            l.setDate(formatter.format(l.getCreatetime()));
        }
                Page<DangerContent> page = (Page<DangerContent>)list;
        return PageUtil.page2pager(page, query);
    }

    /**
     *反馈未处理
     */
    @Override
    public Pager<DangerContent> loadListWFByPage(Query query) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        PageHelper.startPage(query.getPageNum(), query.getNumPerPage(), true);
        List<DangerContent> list = collecterMapper.loadListWFByPage(query);
        for(DangerContent l: list){
            l.setDate(formatter.format(l.getCreatetime()));
        }
        Page<DangerContent> page = (Page<DangerContent>)list;
        return PageUtil.page2pager(page, query);
    }

    /**
     *反馈已处理
     */
    @Override
    public Pager<DangerContent> loadListYFByPage(Query query) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        PageHelper.startPage(query.getPageNum(), query.getNumPerPage(), true);
        List<DangerContent> list = collecterMapper.loadListYFByPage(query);
        for(DangerContent l: list){
            l.setDate(formatter.format(l.getCreatetime()));
        }
        Page<DangerContent> page = (Page<DangerContent>)list;
        return PageUtil.page2pager(page, query);
    }

    @Override
    public Result insertFlowInfo(FlowInfo flowInfo) {
      FlowInfo flowInfo1=collecterMapper.pdiscl(flowInfo);
        if(!flowInfo.getOpertype().equals("2")&&flowInfo1!=null&&!"".equals(flowInfo1.getId())&&null!=flowInfo1.getId()){
            return new Result(Result.FAILURE,"提交失败!,该数据已被其他管理员处理");
        }else{
            int rt =0 ;
            try{
                collecterMapper.gxflowinfozt(flowInfo);
                rt = collecterMapper.insertFlowInfo(flowInfo);
            }catch (Exception e){
                e.printStackTrace();
            }

            if(rt>0){
                return new Result(Result.SUCC,"提交成功!");
            }else {
                return new Result(Result.FAILURE,"提交失败!");
            }
        }

    }

    @Override
    public FlowInfo getDangerContentByIdTime(String id) {
        return  collecterMapper.getDangerContentByIdTime(id);
    }

    @Override
    public FlowInfo getYFkDetail(String id) {
        return  collecterMapper.getYFkDetail(id);
    }

    @Override
    public List<Map<String, Object>> getPhotoFKByDCId(String id) {
        return  collecterMapper.getPhotoFKByDCId(id);
    }

    @Override
    public List<Map<String, Object>> getPhotoBytypeAndId(String type, String id) {
        DangerContent dangerContent=new DangerContent();
        dangerContent.setId(id);
        dangerContent.setChildtype(type);
        return  collecterMapper.getPhotoBytypeAndId(dangerContent);
    }

    @Override
    public String getYHByCode(String type) {
        return  collecterMapper.getYHByCode(type);
    }

    @Override
    public Result insertCfcs(Cfcs cfcs) {
        try {
            collecterMapper.insertcf(cfcs);
        }catch (Exception e) {
            e.printStackTrace();
            return new Result(Result.FAILURE,"提交失败!");
        }
        return new Result(Result.SUCC,"提交成功!");
    }

    @Override
    public Result insertZgcs(Zgcs zgcs) {
        try {
           collecterMapper.insertzg(zgcs);
        }catch (Exception e) {
            e.printStackTrace();
            return new Result(Result.FAILURE,"提交失败!");
        }
        return new Result(Result.SUCC,"提交成功!");
    }

    @Override
    public Cfcs getCfcs(String id) {
        try{
            return collecterMapper.getonecfcs(id);
        }catch (Exception e){
            e.printStackTrace();
        }
        return  null;
    }

    @Override
    public Zgcs getZgcs(String id) {
        try{
            return collecterMapper.getonezgcs(id);
        }catch (Exception e){
            e.printStackTrace();
        }
        return  null;
    }

    @Override
    public Result deletePic(String url) {
        FlowInfo flowInfo=new FlowInfo();
        flowInfo.setDealphoto0(url);
        try {
            collecterMapper.deletePic(flowInfo);
        }catch (Exception e) {
            e.printStackTrace();
            return new Result(Result.FAILURE,"删除失败!");
        }
        return new Result(Result.SUCC,"删除成功!");
    }
}
